import traceback
from selenium import webdriver
import datetime
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from tools.my_log import MyLog

class BasePage:

    def __init__(self,driver):
        # self.driver = webdriver.Chrome()
        self.driver = driver
        self.log = MyLog()

    def get_element(self,locator,doc):# (By.XPATH,"//*[]")
        res = self.wait_element(locator, doc)
        if res is False:
            return

        self.log.info("开始进行元素%s的定位"%str(locator))
        try:
            start_time = datetime.datetime.now()
            ele = self.driver.find_element(*locator)
            end_time = datetime.datetime.now()
            wait_time = (end_time- start_time).seconds
            self.log.info("元素%s定位成功，耗时%s秒"%(str(locator),wait_time))
            return ele
        except Exception as e:
            self.log.info("元素%s定位失败"%str(locator))
            self.log.info(traceback.format_exc())
            self.get_shot(doc)

    def input_text(self,locator,value,doc):
        """
        输入操作
        :param locator: 元素定位方式(元组)
        :param value: 输入内容
        :return:
        """

        ele = self.get_element(locator,doc)  # None
        self.log.info("元素%s输入操作开始"%str(locator))
        try:
            ele.send_keys(value)
            self.log.info("元素%s输入操作成功" % str(locator))
        except Exception as e:
            self.log.info("元素%s输入操作失败" % str(locator))
            self.log.info(traceback.format_exc())
            self.get_shot(doc)

    def click_element(self,locator,doc):
        """
        点击操作
        :param locator: 元素定位方式(元组格式)
        :return:
        """
        ele = self.get_element(locator,doc)
        try:
            ele.click()
            self.log.info("元素%s点击操作成功" % str(locator))
        except Exception as e:
            self.log.info("元素%s点击操作失败" % str(locator))
            self.log.info(traceback.format_exc())
            self.get_shot(doc)

    def get_shot(self,doc): # 登录模块_登录页面_输入操作
        """
        截图操作
        :param doc: 模块名_页面名称_操作名称
        :return:
        """
        filename = '../test_results/screen_shoots/'+doc+".png"
        self.driver.get_screenshot_as_file(filename)

    def switch_into_frame(self,locator,doc):
        frame = self.get_element(locator,doc)
        try:
            self.driver.switch_to.frame(frame)
            self.log.info("frame框架切换操作成功")
        except Exception as e:
            self.log.info("frame框架切换操作失败")
            self.log.info(traceback.format_exc())
            self.get_shot(doc)

    def setich_outo_frame(self):

        self.driver.switch_to.default_content()

    def select_action(self,locator,doc,index=None,value=None,visible_text=None):
        select = self.get_element(locator,doc)
        select = Select(select)

        if index is not None:
            select.select_by_index(index)

        if value is not None:
            select.select_by_value(value)

        if visible_text is not None:
            select.select_by_visible_text(visible_text)

    def get_element_text(self,locator,doc):
        ele = self.get_element(locator,doc)
        text = ele.text
        return text

    def get_element_attribute_value(self,locator,doc,attribute):
        ele = self.get_element(locator,doc)
        try:
            value = ele.get_attribute(attribute)
            self.log.info("获取元素%s的%s属性值成功"%(str(locator),attribute))
            return value
        except Exception as e:
            self.log.info("获取元素%s的%s属性值失败"%(str(locator),attribute))
            self.log.info(traceback.format_exc())
            self.get_shot(doc)

    def wait_element(self,locator,doc,timeout=20,poll=0.5):
        # 显示等待 timeout = 20  针对的是页面元素
        # 隐式等待 针对的是 页面
        # 强制等待  固定等待， 线程
        self.log.info("开始等待元素%s"%str(locator))
        try:
            WebDriverWait(self.driver,timeout,poll_frequency=poll).until(EC.visibility_of_element_located(locator))
            self.log.info("等待元素%s成功" % (str(locator)))
            return True
        except Exception as e:
            self.log.info("等待元素%s超时"%(str(locator)))
            self.log.info(traceback.format_exc())
            self.get_shot(doc)
            return False
